package cmd

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"github.com/spf13/cobra"
	"os"
)

var VERSION = "dev"
var cfgFile string
var cfgData *rawConf = new(rawConf)
var rootCmd = &cobra.Command{}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func init() {
	rootCmd.Version = VERSION
	cobra.OnInitialize(initConfig)
	rootCmd.PersistentFlags().StringVarP(&cfgFile, "conf", "c", "conf.toml", "config file (default is conf.toml)")
}

func initConfig() {
	parseConfFile()
}

// 解析配置
type serverConf struct {
	LAddr       string
	LogEnv      string
	LogLevel    string
	LogPath     string
	GameRoundMS int64
}

type apiConf struct {
	LAddr string
}

type testConf struct {
	TestGameId  uint64
	TestUserIds []int32
}

type rawConf struct {
	Server *serverConf
	Api    *apiConf
	Test   *testConf
}

func parseConfFile() {
	_, err := toml.DecodeFile(cfgFile, cfgData)
	if err != nil {
		panic(err.Error())
	}
}
